package com.u2b.business.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;




public class TimeService
{
    private final SimpleDateFormat sdp;

    public TimeService()
    {
        sdp = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }

    public void addCreateTime(HashMap<String, Object> value)
    {
        String create_time = getCurrentTime();
        value.put("create_time", create_time);
    }
//   //可能出现显示大于几毫秒的情况
//    public void isExpire(HashMap<String, Object> value)
//    {
//        String display_time = (String) value.get("display_time"); // DisplayTime是已一周为准
//        String create_time = (String) value.get("create_time");
//        try
//        {
//            long current_time = Calendar.getInstance().getTimeInMillis();
//            long past_time = sdp.parse(create_time).getTime() + Integer.valueOf(display_time) * 60 * 60 * 1000;
//            if (current_time > past_time)
//            {
//                value.put("isExpire", true);
//            }
//            else
//            {
//                value.put("remain_time", past_time / 1000 / 3600);
//            }
//        }
//        catch (ParseException e)
//        {
//            e.printStackTrace();
//        }
//
//    }
 //搞错了.可能出现大于几毫秒的情况
    public void isExpire(List<HashMap<String, Object>> values)
    {
        Iterator<HashMap<String, Object>> iterable = values.iterator();
        while (iterable.hasNext())
        {
            HashMap<String, Object> value = iterable.next();
            String display_time =  value.get("display_time").toString() ; // DisplayTime是已一周为准
            String create_time =  value.get("create_time").toString();
            try
            {
                long current_time = Calendar.getInstance().getTimeInMillis();
                long past_time = sdp.parse(create_time).getTime() + Integer.valueOf(display_time) * 60 * 60 * 1000;
                if (current_time > past_time)
                {
                    iterable.remove(); // 同时删掉了原来的数据了
                }
                else
                {
                    value.put("remain_time",(past_time-current_time) / 1000); // 以秒为准
                }
            }
            catch (ParseException e)
            {
                throw new RuntimeException("isExpire出现异常", e);
            }
        }

    }

    private String getCurrentTime()
    {
        String date = sdp.format(Calendar.getInstance().getTime());
        return date;

    }

    public void sortListByCreateTime(List<HashMap<String, Object>> list)
    {

        
        Comparator<HashMap<String, Object>> comp = new HashMapCompare();
        Collections.sort(list, comp);

    }

    // 小于的时候return 1,就能按照从大到小排序
    private class HashMapCompare implements Comparator<HashMap<String, Object>>
    {

        @Override
        public int compare(HashMap<String, Object> o1, HashMap<String, Object> o2)
        {
            int flag = 0;

            String s_time_1 = o1.get("create_time").toString();
            String s_time_2 = o2.get("create_time").toString();

            try
            {
                long time_1 = sdp.parse(s_time_1).getTime();
                long time_2 = sdp.parse(s_time_2).getTime();

                return time_1 < time_2 ? 1 : 0;

            }
            catch (ParseException e)
            {

            }
            return flag;

        }
    }
}
